package com.ys.controller.api.sys;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwWanliOtherPayment;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsSynLog;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.service.sys.YsSynLogService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/ysSynLog")
public class ApiInterYsSynLogController {
	
	@Autowired
	private CommonApiService<TcwWanliOtherPayment> commonApiService;
	@Autowired
	private CommonApiService<YsSynLog> commonApiServiceDetail;
	@Autowired
	private YsSynLogService ysSynLogService;
	
	@RequiresPermissions("ysSynLog:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute YsSynLog cond){
		
		cond = setSeachSql(cond);
		return commonApiServiceDetail.searchLay(cond);
	}
	
	/**
	 * 同步菜单
	 */
	@RequiresPermissions("ysSynLog:syn")
	@PostMapping("/syn")
    @ResponseBody
	public R syn(Integer type){
		
		return ysSynLogService.syn(type);
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("ysSynLog:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute YsSynLog cond) throws Exception{
		
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.YS_SynLog)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TcwWanliOtherPayment> util = new ExportExcelUtils<TcwWanliOtherPayment>(new TcwWanliOtherPayment());
        
        String toFilePath = util.exportExcel(listData, listField, "数据同步记录");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public YsSynLog setSeachSql(YsSynLog cond){
		
        return  cond;
    }
}
